package net.sourceforge.jweb.jstl.fn;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class CollectionUtil {
	private CollectionUtil() {
	}

	public static boolean contains(Collection<Object> a, Object b) {
		if (a == null)
			return false;
		return a.contains(b);
	}

	public static boolean isEmpty(Collection<Object> a) {
		return a != null && a.isEmpty();
	}

	public static int size(Collection<Object> coll) {
		return coll == null ? 0 : coll.size();
	}

	public static Object[] toArray(Collection<Object> coll) {
		return coll == null ? new Object[] {} : coll.toArray();
	}

	public static boolean add(Collection<Object> coll, Object obj) {
		return coll == null ? false : coll.add(obj);
	}

	public static boolean remove(Collection<Object> coll, Object obj) {
		return coll == null ? false : coll.remove(obj);
	}

	public static boolean containsAll(Collection<Object> coll, Collection<?> c) {
		return coll == null ? false : coll.containsAll(c);
	}

	public static boolean addAll(Collection<Object> coll, Collection<?> c) {
		return coll == null ? false : coll.addAll(c);
	}

	public static boolean removeAll(Collection<Object> coll, Collection<?> c) {
		return coll == null ? false : coll.removeAll(c);
	}

	public static boolean retainAll(Collection<Object> coll, Collection<?> c) {
		return coll == null ? false : coll.retainAll(c);
	}

	public static void clear(Collection<Object> coll) {
		if (coll != null)
			coll.clear();
	}

	public static boolean equals(Collection<Object> coll, Object obj) {
		return coll == null ? obj == null : coll.equals(obj);
	}

	public static int hashCode(Collection<Object> coll) {
		return coll == null ? -1 : coll.hashCode();
	}

	// list
	public static boolean addAll(List<Object> coll, int index, Collection<?> c) {
		return coll == null ? false : coll.addAll(index, c);
	}

	public static Object get(List<Object> coll, int index) {
		return coll == null ? null : coll.get(index);
	}

	public static Object set(List<Object> coll, int index, Object element) {
		return coll == null ? null : coll.set(index, element);
	}

	public static void add(List<Object> coll, int index, Object element) {
		if (coll != null)
			coll.add(index, element);
	}

	public static Object remove(List<Object> coll, int index) {
		return coll == null ? null : coll.remove(index);
	}

	public static int indexOf(List<Object> coll, Object o) {
		return coll == null ? -1 : coll.indexOf(o);
	}

	public static int lastIndexOf(List<Object> coll, Object o) {
		return coll == null ? -1 : coll.lastIndexOf(o);
	}

	public static List<Object> subList(List<Object> coll, int fromIndex, int toIndex) {
		return coll == null ? new ArrayList<Object>(1) : coll.subList(fromIndex, toIndex);
	}

}
